import api from "@/http/api";
import {getTimestamp} from "@/http/tools";

/**
 * 获取国家列表
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getCountryList() {
    return await api({
        url: '/countries/code/list',
        method: 'get'
    })
}

/**
 * 获取一个二维码key
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getQRKey() {
    return await api({
        url: '/login/qr/key',
        method: 'get',
        params: {
            timestamp: getTimestamp()
        }
    })
}

/**
 * 轮询询问二维码状态
 * @param key
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getQRCodeStatus(key) {
    return await api({
        url: '/login/qr/check',
        method: 'get',
        params: {
            timestamp: getTimestamp(),
            key
        }
    })
}

/**
 * 获取登录二维码图片
 * @param key
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getQRImg(key) {
    return await api({
        url: '/login/qr/create',
        method: 'get',
        params: {
            timestamp: getTimestamp(),
            key,
            qrimg: true
        }
    })
}

/**
 * 发现页获取数据
 * @param params    参数，refresh是否刷新数据，cursor
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getFindPage(params = {}) {
    return await api({
        url: '/homepage/block/page',
        params,
        method: 'get'
    })
}

/**
 * 获取登录状态
 * @param cookie
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getLoginStatus() {
    return await api({
        url: '/login/status',
        params: {
            timestamp: getTimestamp(),
        },
        method: 'get'
    })
}

/**
 * 发送验证码
 * @param phone 手机号
 * @param ctcode    国家区号，默认为86
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getVrCode(phone, ctcode = 86) {
    return await api({
        url: '/captcha/sent',
        params: {
            phone,
            ctcode,
            timestamp: getTimestamp()
        },
        method: 'get'
    })
}

/**
 * 验证验证码是否正确
 * @param phone 手机号
 * @param captcha   验证码
 * @param countrycode 国家码
 * @return {Promise<*>}
 */
export async function checkVrCode(phone, captcha, countrycode = 86) {
    return await api({
        url: '/login/cellphone',
        params: {
            phone,
            captcha,
            countrycode,
            timestamp: getTimestamp(),
        },
        method: 'get'
    })
}

/**
 * 获取用户账号信息
 * @return {Promise<*>}
 */
export async function getUserAccountInfo() {
    return await api({
        url: '/user/account',
        params: {
            timestamp: getTimestamp()
        },
        method: 'get'
    })
}

/**
 * 获取推荐歌单（不需要登录）
 * @param limit 获取数量
 * @return {Promise<*>}
 */
export async function getRecPlayList(limit) {
    return await api({
        url: '/personalized',
        params: {
            limit
        },
        method: 'get'
    })
}

/**
 * 获取全部新碟
 * @param limit 获取数量
 * @param offset    偏移量
 * @param area      区域 ALL:全部,ZH:华语,EA:欧美,KR:韩国,JP:日本
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getAlbum(limit, offset = 0, area = 'ALL') {
    return await api({
        url: '/album/new',
        params: {
            limit,
            offset,
            area
        },
        method: 'get'
    })
}

/**
 * 获取热门歌手
 * @param limit
 * @return {Promise<*>}
 */
export async function getSinger(limit) {
    const offset = Math.floor(Math.random() * 40)
    return await api({
        url: '/top/artists',
        params: {
            limit,
            offset
        },
        method: 'get'
    })
}

/**
 * 获取所有榜单
 * @return {Promise<*>}
 */
export async function getTopList() {
    return await api({
        url: '/toplist',
        method: 'get'
    })
}

/**
 * 检查音乐是否可用
 * @param id    音乐的id
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function checkMusic(id) {
    return await api({
        url: '/check/music',
        params: {
            id
        },
        method: 'get'
    })
}

/**
 * 获取音乐的具体信息
 * @param id    音乐的id
 * @param level 音质  standard => 标准,higher => 较高, exhigh=>极高, lossless=>无损, hires=>Hi-Res
 * @return {Promise<axios.AxiosResponse<*>>}
 */
export async function getMusicInfo(id, level = 'standard') {
    return await api({
        url: '/song/url/v1',
        params: {
            id,
            level
        },
        method: 'get'
    })
}

/**
 * 获取歌单详情
 * @param id    歌单id
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getPlayListDetail(id) {
    return await api({
        url: '/playlist/detail',
        params: {
            id
        }
    })
}

/**
 * 获取指定歌单所有的歌曲
 * @param id    歌单id
 * @param limit 每次获取的数量
 * @param offset    从第几首歌开始
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getPlayListAllSongs(id, limit = 0, offset = 0) {
    return await api({
        url: '/playlist/track/all',
        params: {
            id,
            limit,
            offset
        },
        method: 'get'
    })
}

/**
 * 获取歌曲详情
 * @param ids
 * @return {Promise<*>}
 */
export async function getMusicDetail(ids) {
    return await api({
        url: '/song/detail',
        params: {
            ids
        },
        method: 'get'
    })
}

/**
 * 获取专辑的内容
 * @param id    专辑id
 * @return {Promise<*>}
 */
export async function getAlbumContent(id) {
    return await api({
        url: '/album',
        params: {
            id
        },
        method: 'get'
    })
}

/**
 * 获取歌手的专辑
 * @param id    歌手id
 * @param limit
 * @return {Promise<axios.AxiosResponse<any>>}
 */
export async function getArtistAlbums(id, limit = 5) {
    return await api({
        url: '/artist/album',
        params: {
            id,
            limit
        },
        method: 'get'
    })
}

// 获取歌单分类信息
export const getCatList = async () => {
    return await api({
        url: '/playlist/catlist',
        method: 'get'
    })
}

// 获取指定类别的歌单
export const getSpecialSongList = async (cat, offset = 0, order = 'hot', limit = 30) => {
    return await api({
        url: '/top/playlist',
        method: 'get',
        params: {
            cat,
            offset,
            order,
            limit
        }
    })
}

// 获取精品歌单
export const getHighqualitySongList = async (cat, before = 0, limit = 30) => {
    return await api({
        url: '/top/playlist/highquality',
        method: 'get',
        params: {
            cat,
            // 上一次获取数据的updateTime
            before,
            limit
        }
    })
}

/**
 * 获取歌手的详细信息
 * @param id    歌手的id
 * @returns {Promise<*>}
 */
export const getSingersDetailInfo = async id => {
    return await api({
        url: '/artist/detail',
        method: 'get',
        params: {
            id
        }
    })
}

// 获取歌手专辑信息
export const getSingersAlbum = async (id, limit = 30, offset = 0) => {
    return await api({
        url: '/artist/album',
        method: 'get',
        params: {
            id,
            limit,
            offset
        }
    })
}

// 获取歌手的mv
export const getSingersMvs = async (id, limit = 10, offset = 0) => {
    return await api({
        url: '/artist/mv',
        method: 'get',
        params: {
            id,
            limit,
            offset
        }
    })
}

// 获取歌手的热门50首歌曲
export const getSingerHotSong = async id => {
    return await api({
        url: '/artist/top/song',
        method: 'get',
        params: {
            id
        }
    })
}

// 获取mv的详细信息
export const getMvsInfo = async mvid => {
    return await api({
        url: '/mv/detail',
        method: 'get',
        params: {
            mvid
        }
    })
}
/**
 * 获取mv的链接
 * @param id    mv的id
 * @param r     分辨率，从mv详细信息中获得而来
 * @returns {Promise<*>}
 */
export const getMvsUrl = async (id, r) => {
    return await api({
        url: '/mv/url',
        method: 'get',
        params: {
            id,
            r
        }
    })
}

// 获取相似的mv
export const getSimiMvs = async (mvid) => {
    return await api({
        url: '/simi/mv',
        method: 'get',
        params: {
            mvid
        }
    })
}

// 获取指定地区的歌手榜单
export const getSingerLevel = async (type) => {
    return await api({
        url: '/toplist/artist',
        method: 'get',
        params: {
            type
        }
    })
}

/**
 * 获取分类歌手列表
 * @param type  歌手类型
 * @param area  区域
 * @param initial   首字母筛选
 * @param limit     获取多少条数据
 * @param offset    分页偏移
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getCatSingers = async (type, area, initial, limit = 30, offset = 0) => {
    return await api({
        url: '/artist/list',
        method: 'get',
        params: {
            type,
            area,
            initial,
            limit,
            offset
        }
    })
}

// 获取搜索建议
export const getSuggest = async (keywords) => {
    return await api({
        url: '/search/suggest',
        method: 'get',
        params: {
            keywords
        }
    })
}

// 用户退出登录
export const userLogout = async () => {
    return await api({
        url: '/logout',
        method: 'get',
        params: {
            timestamp: getTimestamp()
        }
    })
}